<?php
class Application_Model_QueueResources extends Xend_Db_Model {
	protected $_dbTableName = 'Application_Model_Table_QueueResources';

	public function __construct() {
		parent::__construct();
	}

	/**
	 * speichert einen neuen Eintrag von in der Queue benoetigte Resource in die Tabelle
	 * queue_resources
	 * @param Application_Model_Schema_QueueResource $resource
	 * @param int Id des zuletzt abgespeicherten Datensatzes
	 */
	public function save(Application_Model_Schema_QueueResource $resource) {
		$data = array(
			'queue_id'	=> $resource->queue->id
		);

		if($resource->resource instanceof Application_Model_Schema_TileResource) {
			$data['tile_resources_id'] = $resource->resource->id;
		}

		if($resource->resource instanceof Application_Model_Schema_StorageResource) {
			$data['storage_resources_id'] = $resource->resource->id;
		}

		return (int) $this->getDbTable()->insert($data);
	}

	/**
	 * speichert neue Eintraege von in der Queue benoetigten Resourcen in die Tabelle
	 * queue_resources
	 * @param array $data mit Eintraegen aus Application_Model_Schema_QueueResource
	 * @param none
	 */
	public function saveall($data) {
		foreach($data as $queueResource) {
			$this->save($queueResource);
		}
	}

	/**
	 * liesst alle Resouren aus, die einer Queue zugeordnet sind
	 * @param array $where Array mit Bedingungen, die an die Datenbank uebergeben werden
	 * @param array $ref Array mit Tabellen, die verknuepft werden sollen
	 * @return array Array mit den Daten, die aus der Datenbank geladen wurden
	 */
	public function findall($where = array(), $ref = array()) {
		$queueResources	= array();
		$ref						= array_flip($ref);
		$select					= $this->_database
			->select()
			->from(
				array('queue_resources'),
        array('queue_resources.id', 'queue_resources.queue_id', 'queue_resources.tile_resources_id', 'queue_resources.storage_resources_id')
			)
			->order(array('queue_resources.id'));

		// Where
		// -----------------------------------------------------------------------------------
		if(is_array($where) && !empty($where)) {
			foreach($where as $k => $v) {
				$select->where($k, $v);
			}
		}

		// Datenauswertung
		// -----------------------------------------------------------------------------------
		foreach($this->fetch($select) as $k => $v) {
			$queueResource	= new Application_Model_Schema_QueueResource();

			// ---------------------------------------------------------------------------------
			// Queue
			$queue = new Application_Model_Schema_Queue();
			$queue->setId($v['queue_resources']['queue_id']);

			// ---------------------------------------------------------------------------------
			// QueueResources
			if(isset($v['queue_resources']['tile_resources_id'])) {
				$resource = new Application_Model_Schema_TileResource();
				$resource->setId($v['queue_resources']['tile_resources_id']);
			}
			if(isset($v['queue_resources']['storage_resources_id'])) {
				$resource = new Application_Model_Schema_StorageResource();
				$resource->setId($v['queue_resources']['storage_resources_id']);
			}

			// ---------------------------------------------------------------------------------
			// Zuordnung
			$queueResource
				->setId($v['queue_resources']['id'])
				->setQueue($queue)
				->setResource($resource);

			$queueResources[(int) $v['queue_resources']['id']] = $queueResource;
		}

		// -----------------------------------------------------------------------------------
		// Referenzen verknuepfen
		if(!empty($ref)) {
			if(array_key_exists('Resources', $ref)) {
				$queueResources = $this->_assignResourceReferences($queueResources);
			}
		}

		return $queueResources;
	}

	/**
	 * verknuepft die, per Array uebergebenen, Queue-Resource-Eintraege mit den Daten der
	 * referenzierten Resourcen (momentan nur TileResourcen)
	 * @param array $sources Array mit den Daten der Queue-Resourcen
	 * @param array $references optionales Array mit den UID der Resourcen
	 * @return array Array der Queue-Eintraege mit den verknuepften Aktion-Daten
	 */
	protected function _assignResourceReferences($sources, $references = array()) {

		// falls nicht uebergeben, erstelle ein Reference-Array mit den UIDs der Resourcen
		// die geladen werden sollen
		if(empty($references)) {
			$references = array(
				'TileResources'			=> array(),
				'StorageResources'	=> array()
			);

			foreach($sources as $source) {
				if($source->resource instanceof Application_Model_Schema_TileResource) {
					$references['TileResources'][] = $source->resource->id;
				}

				if($source->resource instanceof Application_Model_Schema_StorageResource) {
					$references['StorageResources'][] = $source->resource->id;
				}
			}
		}

		// lade referenzierte TileResourcen
		$tileResourceData = array();
		if(!empty($references['TileResources'])) {
			$tileResourceModel	= new Application_Model_TileResources();
			$tileResourceData		= $tileResourceModel->findall(array('tile_resources.id IN (?)' => $references['TileResources']));
		}

		// lade referenzierte StorageResourcen
		$storageResourceData = array();
		if(!empty($references['StorageResources'])) {
			$storageResourceModel	= new Application_Model_StorageResources();
			$storageResourceData	= $storageResourceModel->findall(array('storage_resources.id IN (?)' => $references['StorageResources']));
		}

		// Resourcen dem Source (QueueResource) zuordnen
		foreach($sources as $source) {
			if(array_key_exists($source->resource->id, $tileResourceData) && $source->resource instanceof Application_Model_Schema_TileResource) {
				$source->setResource($tileResourceData[$source->resource->id]);
			}

			if(array_key_exists($source->resource->id, $storageResourceData) && $source->resource instanceof Application_Model_Schema_StorageResource) {
				$source->setResource($storageResourceData[$source->resource->id]);
			}
		}

		return $sources;
	}
}
?>